﻿using UnityEngine.Networking;


namespace HEFramework
{
    /// <summary>
    ///
    /// 安卓Streaming模式文件流
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2024-01-06 20:25:12
    /// ----------------------------------------
    /// </summary>
    public class AndroidStreamingFileSteam : IFileStream
    {
        public void WriteBytesSyn(string _filePath, byte[] _bytes)
        {
            //不能写入不处理
        }

        public async HETask WriteBytesAsync(string _filePath, byte[] _bytes, HECancellationToken _cancellationToken = default, int _bufferSize = 4096)
        {
            //不能写入不处理
        }

        public byte[] ReadBytesSyn(string _filePath)
        {
            var uri = new System.Uri(System.IO.Path.Combine(_filePath));
            UnityWebRequest request = UnityWebRequest.Get(_filePath);
            request.SendWebRequest();
            if (request.error == null)
            {
                while (true)
                {
                    if (request.downloadHandler.isDone)
                    {
                        return request.downloadHandler.data;
                    }
                }
            }
            else
            {
                return null;
            }
        }

        public async HETask<byte[]> ReadBytesAsync(string _filePath, HECancellationToken _cancellationToken = default, int _bufferSize = 4096)
        {
            var uri = new System.Uri(System.IO.Path.Combine(_filePath));
            UnityWebRequest request = UnityWebRequest.Get(_filePath);
            await request.SendWebRequest();
            if (request.error == null)
            {
                return request.downloadHandler.data;
            }
            else
            {
                return null;
            }
        }

        public bool CheckFile(string _filePath)
        {
            //不能写入不处理
            return false;
        }

        public bool DeleteFile(string _filePath)
        {
            //不能写入不处理
            return false;
        }

        public bool CreateDirectory(string _path)
        {
            //不能写入不处理
            return false;
        }

        public bool DeleteDirectory(string _path)
        {
            //不能写入不处理
            return false;
        }
    }
}